{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from server.knowledge_base.kb_service.base import KBServiceFactory\n",
    "from server.knowledge_base.kb_doc_api import search_docs\n",
    "from server.knowledge_base.model.kb_document_model import DocumentWithVSId\n",
    "from PyPDF2 import PdfReader\n",
    "from server.knowledge_base.kb_doc_api import recreate_vector_store\n",
    "from server.knowledge_base.utils import (validate_kb_name, list_files_from_folder, get_file_path,\n",
    "                                         files2docs_in_thread, KnowledgeFile)\n",
    "from server.knowledge_base.kb_api import list_kbs, create_kb, delete_kb\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m/home/FAST_DATA_MIRROR/Langchain-Chatchat-master/server/knowledge_base/kb_api.py:12 : list_kbs\u001b[0m\n",
      "200\n",
      "success\n",
      "['zszt1', 'papers']\n"
     ]
    }
   ],
   "source": [
    "# 获取知识库列表\n",
    "res = list_kbs()\n",
    "print(res.code)\n",
    "print(res.msg)\n",
    "print(res.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-03-14 14:24:57,187 - faiss_cache.py[line:94] - INFO: loading vector store in 'zszt1/vector_store/bge-large-zh-v1.5' from disk.\n",
      "/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "2024-03-14 14:24:58,026 - SentenceTransformer.py[line:66] - INFO: Load pretrained SentenceTransformer: BAAI/bge-large-zh-v1.5\n",
      "2024-03-14 14:24:58,927 - loader.py[line:54] - INFO: Loading faiss with AVX2 support.\n",
      "2024-03-14 14:24:58,942 - loader.py[line:56] - INFO: Successfully loaded faiss with AVX2 support.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "page_content:  标定系统配置\n",
      "source:  相机管理和标定系统配置.pdf\n",
      "id:  d0fab614-7884-42c9-b34f-72f83c3bd6b7\n",
      "docs:  217\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:121: UserWarning: Normalizing L2 is not applicable for metric type: METRIC_INNER_PRODUCT\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "# 获取指定知识库的内容\n",
    "kb = KBServiceFactory.get_service_by_name('zszt1')\n",
    "docs = kb.list_docs()\n",
    "d0 = docs[0]\n",
    "print('page_content: ',d0.page_content.split('\\n')[0])\n",
    "print('source: ',d0.metadata['source'].split('\\n')[0])\n",
    "print('id: ',d0.id)\n",
    "print('docs: ', len(docs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m/home/FAST_DATA_MIRROR/Langchain-Chatchat-master/server/knowledge_base/kb_service/base.py:183 : search_docs\u001b[0m\n",
      "query : 衣服颜色\n",
      "self.do_search 的实际位置 : /home/FAST_DATA_MIRROR/Langchain-Chatchat-master/server/knowledge_base/kb_service/faiss_kb_service.py\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Batches: 100%|██████████| 1/1 [00:00<00:00,  2.31it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m/home/FAST_DATA_MIRROR/Langchain-Chatchat-master/server/knowledge_base/kb_service/faiss_kb_service.py:69 : do_search\u001b[0m\n",
      "\u001b[1;31min do_search query \u001b[0m: 衣服颜色\n",
      "vs.similarity_search_with_score_by_vector 的实际位置 : /home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:301 : similarity_search_with_score_by_vector\u001b[0m\n",
      "\u001b[1;31min similarity_search_with_score_by_vector vector \u001b[0m: (1, 1024)\n",
      "\u001b[1;31min similarity_search_with_score_by_vector scores \u001b[0m: [[0.94473636 0.98695946 1.0445755  1.0623133  1.0912288  1.1189787\n",
      "  1.1372951  1.1434007  1.1809936  1.1816821 ]]\n",
      "\u001b[1;31min similarity_search_with_score_by_vector indices \u001b[0m: [[135 214 213 215 209 136 195 124 137 125]]\n",
      "filter is None\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 7dc4bc1c-6703-4291-bce8-32d38d4556cf, doc: 帽子得分 \\n hairtype \\n String \\n 发型(见行人发型字典) \\n hairScore \\n Float \\n 发型得分 \\n coatcolor \\n String \\n 上衣颜色(见行人上衣颜色字典) \\n coatcolorScore \\n Float \\n 上衣颜色得分 \\n coattype \\n String \\n 上衣类型(见行人上衣类型字典) \\n coattypeScore \\n Float \\n 上衣类型得分 \\n trousecolor \\n String \\n 裤子颜色(见行人裤子颜色字典) \\n trousecolorScore \\n Float \\n 裤子颜色得分 \\n trousetype\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 8c35dc8c-e5c3-43cb-8e78-7a6bf5efa5db, doc: 名称 \\n 1 \\n 蓝牌 \\n 2 \\n 白牌 \\n 3 \\n 黄牌单层 \\n 4 \\n 黄牌双层 \\n 5 \\n 绿牌 \\n 6 \\n 黄绿牌 \\n 7 \\n 黑牌 \\n 8 \\n 其他 \\n 4.2.3 车辆颜色字典 \\n 数值 \\n 名称 \\n 1 \\n 蓝色 \\n 2 \\n 绿色 \\n 3 \\n 黑色 \\n 4 \\n 紫色 \\n 5\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 99d99553-91ea-4709-8caa-551b31f2c0e1, doc: 排队超限 \\n 0x80001(524289) \\n 机动车驶离 \\n 0x100000(1048576) \\n 危险驾驶 \\n 0x200000(2097152) \\n 未戴头盔驾驶 \\n 0x400000(4194304) \\n 排队溢出 \\n 0x800000(8388608) \\n 高温 \\n 4.2.2 车牌颜色字典 \\n 数值 \\n 名称 \\n 1 \\n 蓝牌 \\n 2 \\n 白牌 \\n 3\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 30a45718-3e07-4787-8862-b0fd81f2d58c, doc: 绿色 \\n 3 \\n 黑色 \\n 4 \\n 紫色 \\n 5 \\n 红色 \\n 云鹰平台系统接口标准 \\n 28 \\n 6 \\n 黄色 \\n 7 \\n 白色 \\n 8 \\n 灰色 \\n 9 \\n 棕色 \\n 10 \\n 粉色 \\n 11 \\n 其他 \\n 4.3 硬件故障字典 \\n 4.3.1 故障字典 \\n 数值 \\n 名称 \\n 1 \\n CPU 温度过高 \\n 2\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 90c7333e-5605-4e10-a512-4239ab05f4cf, doc: 小动物 \\n 0x91B(2331) \\n 未穿反光 \\n 0x91C(2332) \\n 短袖 \\n 0x91D(2333) \\n 长袖 \\n 0x91E(2334) \\n 灭火器移位 \\n 0x91F(2335) \\n 遗留物 \\n 0x920(2336) \\n 占道经营 \\n 0x921(2337) \\n 垃圾堆积 \\n 0x922(2338) \\n 垃圾满溢 \\n 0x923(2339)\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 81f7138d-3790-4756-b75f-321623c2d360, doc: trousecolorScore \\n Float \\n 裤子颜色得分 \\n trousetype \\n String \\n 裤子类型(见行人裤子类型字典) \\n trousetypeScore \\n Float \\n 裤子类型得分 \\n bagtype \\n String \\n 背包类型(见行人背包类型字典) \\n bagtypeScore \\n Float \\n 背包类型得分 \\n racetype \\n String \\n 种族类型(见行人种族类型字典) \\n racetypeScore \\n Float \\n 民族得分 \\n agetype \\n String \\n 年龄类型(见行人年龄类型字典) \\n agetypeScore\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 079a60e8-5a77-4f95-8b26-be2cb1e3fa4c, doc: 23 \\n \"pageNum\": null, \\n \"organizationId\": null, \\n \"organizationName\": null, \\n \"id\": 1,      //标签 ID \\n \"name\": \"名称修改\",      //标签名称 \\n \"type\": \"2\",      //标签类型 1 工服 2 人脸 \\n \"createTime\": \"2022-02-02 11:11:11\",      //创建时间 \\n \"picList\": [ \\n { \\n \"id\": 10,      //图片 ID \\n \"url\": \\n \"https://img2.baidu.com/it/u=4147884680,3389833900&fm=253&fmt=auto&app=138&f=JPE\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 74c1cbf5-22b2-4683-bd91-2c3da18eb7ec, doc: motobikecolor \\n String \\n 摩托车车辆颜色(见车辆颜色字典) \\n peopleNumber \\n Int \\n 驾乘人数 \\n gender \\n String \\n 驾乘人员性别(见行人性别字典) \\n agetype \\n String \\n 驾乘人员年龄类型(见行人年龄类型字典) \\n coatcolor \\n String \\n 驾乘人员上衣颜色(见行人上衣颜色字典) \\n coattype \\n String \\n 驾乘人员上衣类型(见行人上衣类型字典) \\n 云鹰平台系统接口标准 \\n 11 \\n 字段名称 \\n 类型 \\n 说明 \\n trousecolor\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: b7a5f2dc-1373-45d4-a291-e456138e788b, doc: 民族得分 \\n agetype \\n String \\n 年龄类型(见行人年龄类型字典) \\n agetypeScore \\n Float \\n 年龄段得分 \\n persontype \\n String \\n 行人类型(见行人类型字典) \\n persontypeScore \\n Float \\n 行人类型得分 \\n clothtexture \\n String \\n 衣服纹理(见行人衣服纹理字典) \\n clothtextureScore \\n Float \\n 衣服纹理得分 \\n direction \\n String \\n 行人朝向(见行人朝向字典) \\n directionScore \\n Float\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:341 \u001b[0m _id: 6d8d0165-3119-420e-ac97-35e6d5b7ce7e, doc: 11 \\n 字段名称 \\n 类型 \\n 说明 \\n trousecolor \\n String \\n 驾乘人员裤子颜色(见行人裤子颜色字典) \\n trousetype \\n String \\n 驾乘人员裤子类型(见行人裤子类型字典) \\n faceCount \\n Int \\n 摩托车上人脸的数量 \\n faceInfo \\n Array \\n 摩托车上人脸信息数组 \\n faceposLeft \\n Int \\n 人脸位置坐标 left \\n faceposTop \\n Int \\n 人脸位置坐标 top \\n faceposRight \\n Int \\n 人脸位置坐标 right\n",
      "\u001b[1;31m/home/liyaze/anaconda3/envs/longchain/lib/python3.11/site-packages/langchain_community/vectorstores/faiss.py:351 \u001b[0m score_threshold: 0.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "contents = kb.search_docs(\"衣服颜色\", 10, 0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m/home/FAST_DATA_MIRROR/Langchain-Chatchat-master/server/utils.py:57 : get_ChatOpenAI\u001b[0m model_name: chatglm3-6b\n"
     ]
    }
   ],
   "source": [
    "from server.utils import wrap_done, get_ChatOpenAI\n",
    "from langchain.callbacks import AsyncIteratorCallbackHandler\n",
    "model = get_ChatOpenAI(\n",
    "    model_name='chatglm3-6b',\n",
    "    temperature=0.7,\n",
    "    max_tokens=None,\n",
    "    callbacks=[AsyncIteratorCallbackHandler()],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "history: [History(role='user', content='你好'), History(role='assistant', content='你好👋！我是人工智能助手 ChatGLM3-6B，很高兴见到你，欢迎问我任何问题。'), History(role='user', content='你好'), History(role='assistant', content='你好👋！我是人工智能助手 ChatGLM3-6B，很高兴见到你，欢迎问我任何问题。')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "JSONDecodeError",
     "evalue": "Expecting value: line 1 column 1 (char 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/longchain/lib/python3.11/site-packages/requests/models.py:971\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    970\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 971\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcomplexjson\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloads\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    972\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m JSONDecodeError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    973\u001b[0m     \u001b[38;5;66;03m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[1;32m    974\u001b[0m     \u001b[38;5;66;03m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/longchain/lib/python3.11/site-packages/simplejson/__init__.py:514\u001b[0m, in \u001b[0;36mloads\u001b[0;34m(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, allow_nan, **kw)\u001b[0m\n\u001b[1;32m    510\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m encoding \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m    511\u001b[0m         parse_int \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m parse_float \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m    512\u001b[0m         parse_constant \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_pairs_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    513\u001b[0m         \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m use_decimal \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m allow_nan \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kw):\n\u001b[0;32m--> 514\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_default_decoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    515\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/longchain/lib/python3.11/site-packages/simplejson/decoder.py:386\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[0;34m(self, s, _w, _PY3)\u001b[0m\n\u001b[1;32m    385\u001b[0m     s \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(s, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mencoding)\n\u001b[0;32m--> 386\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraw_decode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    387\u001b[0m end \u001b[38;5;241m=\u001b[39m _w(s, end)\u001b[38;5;241m.\u001b[39mend()\n",
      "File \u001b[0;32m~/anaconda3/envs/longchain/lib/python3.11/site-packages/simplejson/decoder.py:416\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[0;34m(self, s, idx, _w, _PY3)\u001b[0m\n\u001b[1;32m    415\u001b[0m         idx \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[0;32m--> 416\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscan_once\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43midx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_w\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43midx\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mend\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 12\u001b[0m\n\u001b[1;32m     10\u001b[0m response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mpost(api_url, data\u001b[38;5;241m=\u001b[39mpayload)\n\u001b[1;32m     11\u001b[0m \u001b[38;5;66;03m# 获取并解析服务器返回的结果\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m response_json \u001b[38;5;241m=\u001b[39m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjson\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     13\u001b[0m output_text \u001b[38;5;241m=\u001b[39m response_json\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moutput_text\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28mprint\u001b[39m(output_text)\n",
      "File \u001b[0;32m~/anaconda3/envs/longchain/lib/python3.11/site-packages/requests/models.py:975\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    971\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m complexjson\u001b[38;5;241m.\u001b[39mloads(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtext, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m    972\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m JSONDecodeError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    973\u001b[0m     \u001b[38;5;66;03m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[1;32m    974\u001b[0m     \u001b[38;5;66;03m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n\u001b[0;32m--> 975\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m RequestsJSONDecodeError(e\u001b[38;5;241m.\u001b[39mmsg, e\u001b[38;5;241m.\u001b[39mdoc, e\u001b[38;5;241m.\u001b[39mpos)\n",
      "\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
     ]
    }
   ],
   "source": [
    "from server.utils import BaseResponse, get_prompt_template # 制作 prompt\n",
    "from server.chat.utils import History # 制作 History\n",
    "from langchain.prompts.chat import ChatPromptTemplate # prompt 模板\n",
    "from langchain.chains import LLMChain # LLMChain 服务\n",
    "# \n",
    "prompt_template = get_prompt_template(\"knowledge_base_chat\", \"empty\") # prompt 模板\n",
    "history = []  # 空历史\n",
    "input_msg = History(role=\"user\", content=prompt_template).to_msg_template(False) # input_msg 模板\n",
    "chat_prompt = ChatPromptTemplate.from_messages([i.to_msg_template() for i in history] + [input_msg]) # chat_prompt 模板\n",
    "chain = LLMChain(prompt=chat_prompt, llm=model)\n",
    "# 定义请求地址\n",
    "api_url = \"http://localhost:20002\"  # 这个路径可能需要根据实际API接口调整\n",
    "# 准备请求数据，通常包含问题或其他对话内容\n",
    "data = {\n",
    "    \"input_text\": \"你好，能帮我解答一个问题吗？\",\n",
    "}\n",
    "# 将数据转换为JSON格式\n",
    "payload = json.dumps(data)\n",
    "# 发送POST请求\n",
    "response = requests.post(api_url, data=payload)\n",
    "# 获取并解析服务器返回的结果\n",
    "response_json = response.json()\n",
    "output_text = response_json.get(\"output_text\", \"\")\n",
    "print(output_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('云鹰平台服务系统接口协议V1.8.4.pdf', 'rb') as file:\n",
    "    reader = PdfReader(file)\n",
    "    text = ''\n",
    "    for page_num in range(len(reader.pages)):\n",
    "        text += reader.pages[page_num].extract_text()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<sse_starlette.sse.EventSourceResponse at 0x7fd4732b0950>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recreate_vector_store(\"zszt1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "217\n"
     ]
    }
   ],
   "source": [
    "kb = KBServiceFactory.get_service_by_name('zszt1')\n",
    "docs = kb.list_docs()\n",
    "print(len(docs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Batches: 100%|██████████| 1/1 [00:00<00:00, 36.51it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "contents = kb.search_docs(\"衣服颜色\", 10, 0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "longchain",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
